package com.miguo.utils;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.provider.Settings;
import android.view.WindowManager;

import com.miguo.app.AppContext;
import com.miguo.framework.utils.Log;

/*
 * 读写系统设置允许读写系统设置项 -- 设置屏幕亮度
 * 所需权限
 * <uses-permission android:name="android.permission.WRITE_SETTINGS" />
 */
public class BrightnessUtils {
    public static final int MAX_BRIGHTNESS = 255;
    public static final int MIN_BRIGHTNESS = 1;

    public static int getBrightnessMode(Context context) {
        try {
            int rightness_mode = Settings.System.getInt(context.getContentResolver(),
                    "screen_brightness_mode");
            return rightness_mode;
        } catch (Exception Exception) {
            Log.e("获得当前屏幕的亮度模式失败：" + Exception.getMessage());
        }
        return 0;
    }

    public static int getSysScreenBrightness(Context context) {
        try {
            int brightness = Settings.System.getInt(context.getContentResolver(),
                    "screen_brightness");
            return brightness;
        } catch (Exception Exception) {
            Log.e("获得当前系统的亮度值失败：" + Exception.getMessage());
        }
        return 255;
    }

    public static void recoverBrightnessSetting(Context context) {
        ApiPrefs mApiPrefs = AppContext.Prefs();
        setBrightnessMode(context, mApiPrefs.getSysBrightnessMode());
        setSysScreenBrightness(context, mApiPrefs.getSysScreenBrightness());
    }

    public static void setActScreenBrightness(Context context) {
        setBrightnessMode(context, 0);
        setSysScreenBrightness(context, 1);
    }

    public static void setActScreenBrightness(Activity activity, int brightness) {
        WindowManager.LayoutParams LayoutParams = activity.getWindow()
                .getAttributes();
        LayoutParams.screenBrightness = (brightness / 255.0F);
        activity.getWindow().setAttributes(LayoutParams);
    }

    public static void setBrightnessMode(Context context, int brightness_mode) {
        try {
            Settings.System.putInt(context.getContentResolver(),
                    "screen_brightness_mode", brightness_mode);
            return;
        } catch (Exception Exception) {
            Log.e("设置当前屏幕的亮度模式失败：" + Exception.getMessage());
        }
    }

    public static void setNightMode(Context context, boolean Boolean) {
        if (Boolean) {
            setActScreenBrightness(context);
            return;
        }
        recoverBrightnessSetting(context);
    }

    public static void setSysScreenBrightness(Context context, int brightness) {
        try {
            ContentResolver ContentResolver = context.getContentResolver();
            Uri Uri = Settings.System.getUriFor("screen_brightness");
            Settings.System.putInt(ContentResolver, "screen_brightness", brightness);
            ContentResolver.notifyChange(Uri, null);
            return;
        } catch (Exception Exception) {
            Log.e("设置当前系统的亮度值失败：" + Exception.getMessage());
        }
    }


    // 判断是否开启了自动亮度调节
    public static boolean IsAutoBrightness(Context context) {
        boolean IsAutoBrightness = false;
        try {
            IsAutoBrightness = Settings.System.getInt(
                    context.getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        return IsAutoBrightness;
    }

    // 停止自动亮度调节
    public static void stopAutoBrightness(Context context) {
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }

    // 开启亮度自动调节
    public static void startAutoBrightness(Context context) {
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
    }

    // 保存亮度设置状态
    public static void saveBrightness(Context context, int brightness) {
        Uri uri = android.provider.Settings.System
                .getUriFor("screen_brightness");
        android.provider.Settings.System.putInt(context.getContentResolver(),
                "screen_brightness", brightness);
        context.getContentResolver().notifyChange(uri, null);
    }
}